package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.StorySysEmployee;
import com.ruoyi.system.mapper.StorySysEmployeeMapper;
import com.ruoyi.system.service.IStoryService;
import com.ruoyi.system.service.StorySysEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class StorySysEmployeeServiceImpl implements StorySysEmployeeService {
    @Autowired
    private StorySysEmployeeMapper storySysEmployeeMapper;

    @Autowired
    private IStoryService storyService;

    @Autowired
    private TaskServiceImpl taskService;

    @Override
    public int insertStoryAndSysEmployee(StorySysEmployee storySysEmployee) {
        int i = storySysEmployeeMapper.insertStoryAndSysEmployee(storySysEmployee);
        if (i > 0){
            String empImByKeyId = storyService.getEmpImByKeyId(storySysEmployee.getSysEmployeeId());
            taskService.send(empImByKeyId, "handOver");
            return i;
        }

        return 0;
    }

    @Override
    public List<StorySysEmployee> selectNameAndOpinionById(String storyId, int transferNumber) {
        return storySysEmployeeMapper.selectNameAndOpinionById(storyId, transferNumber);
    }

    @Override
    public int determineTransferNumber(int storyId) {
        int count1 = storySysEmployeeMapper.selectTransferNumberByStoryId(storyId, 1);
        int count2 = storySysEmployeeMapper.selectTransferNumberByStoryId(storyId, 2);
        if (count1 == 0){
            return 1;
        }else if (count1 != 0 && count2 == 0) {
            return 2;
        }else {
            return 3;
        }

    }

    @Override
    public List<StorySysEmployee> selectReply(int storyId, int transferNumber) {
        return storySysEmployeeMapper.selectReply(storyId, transferNumber);
    }

    @Override
    public int updateBack(int storyId, int transferNumber) {
        return storySysEmployeeMapper.updateBack(storyId, transferNumber);
    }

    @Override
    public String selectOpinion(int storyId) {
        String opinion = storySysEmployeeMapper.selectOpinion(storyId);
        if (opinion == null){
            return "";
        }else {
            return opinion;
        }
    }

    @Override
    public int updateReplyState(int replyId, int isBack) {
        return storySysEmployeeMapper.updateReplyStateByCompletion(replyId, isBack);
    }

    @Override
    @Transactional
    public int updateHandleState(int storyId, int replyId, int handleState, int isBack, int transferNumber) {
        //1 先更新处理信息的返回状态为 2（2为完结状态）
        int count = updateReplyState(replyId, isBack);

        //2 判断这条小事的所有移交的处理的状态是否都不为0，都不为0的话就需要更新状态  都为2（完结）小事的handleState为8  有一个为1（退回）小事的handleState为4
        if (count != 0){
            List<StorySysEmployee> storySysEmployees = storySysEmployeeMapper.selectIsBackByStoryIdAndTransferNumber(storyId, transferNumber);
            int first = storySysEmployees.get(0).getIsBack();
            Boolean flag = true;  //判断数组的元素是否全部相同
//            Boolean isZero = false; //判断有没有isBack为0的
            Boolean back = false;
            for (StorySysEmployee storySysEmployee : storySysEmployees) {
                if (first != storySysEmployee.getIsBack()){
                    flag = false;//isBack的状态不全部相同
                }
//                if (storySysEmployee.getIsBack() == 0){
//                    isZero = true;
//                }
                if (storySysEmployee.getIsBack() == 1){
                    back = true;
                }
            }

            //状态都为0 或者有一个为0，则不需要更新状态
            if (flag == true && first == 2){//状态都为2 ，表示都完结了，需要更新小事状态
                storyService.updateHandleStateByCompletion(storyId, handleState);
            }else if (back){//一个退回即退回
                storyService.updateHandleStateAndHandleTime(storyId, handleState, null);
            }

            //每一个操作都需要去添加时间
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            storySysEmployeeMapper.updateOperationTime(replyId, dateFormat.format(now));

        }

        return count;
    }

    @Override
    public void cuiBan(String storyId, int transferNumber) {
        List<String> sysEmployeeIds = storySysEmployeeMapper.selectEmpLastOne(Integer.parseInt(storyId), transferNumber);
        for (String sysEmployeeId : sysEmployeeIds) {
            System.out.println("sysEmployeeId = " + sysEmployeeId);
            sysEmployeeId = storyService.getEmpImByKeyId(sysEmployeeId);
            String cuiBan = "cuiBan";
            taskService.send(sysEmployeeId, cuiBan);
        }

    }


}
